package com.sanpu.kxe.module.dialog;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseDialog;

import butterknife.ButterKnife;

/**
 * Description：微信公众号/订阅号联系
 * Created by 薛勇军 on 2018/7/12.
 */

public class WechatServiceDialog extends BaseDialog<WechatServiceDialog> {
    private TextView close;

    private LinearLayout vContent;
    private TextView vTitle;
    private String title;
    private View contentView;
    private Context context;

    private ClipboardManager mClipboard = null;
    private ClipData mClip;
    private String mClipStr;

    public WechatServiceDialog content(View content) {
        this.contentView = content;
        return this;
    }

    public WechatServiceDialog(Context context, String clipStr) {
        super(context);
        this.context = context;
        this.mClipStr = clipStr;
        mClipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    }

    @Override
    public View onCreateView() {
        showAnim(new BaseAnimatorSet() {
            @Override
            public void setAnimation(View view) {

            }
        });
        View inflate = View.inflate(getContext(), R.layout.dialog_wechat_service, null);
        vContent = ButterKnife.findById(inflate, R.id.content);
        vTitle = ButterKnife.findById(inflate, R.id.title);
        close = ButterKnife.findById(inflate, R.id.close);
        inflate.setPadding(dp2px(15), dp2px(15), dp2px(15), dp2px(15));

        ButterKnife.findById(inflate, R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        ButterKnife.findById(inflate, R.id.open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                openWechat();
            }
        });
        if (!TextUtils.isEmpty(title)) {
            vTitle.setVisibility(View.VISIBLE);
            vTitle.setText(title);
        }
        return inflate;
    }

    private void openWechat() {
        try {
            mClip = ClipData.newPlainText("", mClipStr);
            mClipboard.setPrimaryClip(mClip);

            Intent intent = new Intent();
            ComponentName cmp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(cmp);
            ((Activity) context).startActivityForResult(intent, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setUiBeforShow() {
        vContent.addView(contentView);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public WechatServiceDialog title(String title) {
        this.title = title;
        return this;
    }
}